<template>
	<uni-nav-bar title="合伙人" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
	<view class="package">
		<input class="uni-input" confirm-type="search" placeholder="搜索手机号/姓名" @confirm="confirm" v-model="search"
			@input="interrogate" />
	</view>
	<scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
		:style="{ height: pageHeight + 'px' }">
		<FriendList :frinedList="frinedList" @jump="jump"></FriendList>
	</scroll-view>
	<view @click="navTo('administrator/partner/addPartner')" class="add">添加合伙人</view>
</template>

<script setup lang="ts">
	import { navBack, navTo } from '@/utils/navigator';
	import { onShow } from '@dcloudio/uni-app';
	import { ref } from 'vue';
	import FriendList from '@c/administrator/List/friendList.vue';
	import { toPublish } from '@mqtt';
	import { getPatnerList } from '@/gql/administrator';
	import { showLoading, errorToast } from '@/utils/prompt';
	const search = ref('')
	const inquireAbout = ref(false)
	const pageHeight = ref();
	const frinedList = ref([])
	const lastPage = ref(1);
	const currentPage = ref(1);
	uni.getSystemInfo({
		success: function (res) {
			pageHeight.value = res.windowHeight - 180;
		},
	});
	onShow(() => {
		frinedList.value = []
		init()
	})
	/**
	  * 获取车友列表
	  */
	function init() {
		showLoading()
		const payload = {
			query: getPatnerList,
			variables: {
				keywords: search.value,
			},
		};
		toPublish(
			'ql/control/getPatnerList',
			payload,
			(obj : any) => {
				uni.hideLoading();
				const { getPatnerList } = obj.data;
				frinedList.value = getPatnerList
			}
		);
	}
	/**
	 * 加载更多
	 */
	function loadMore() {
		if (currentPage.value < lastPage.value) {
			currentPage.value++;
			init();
		}
	}
	/**
	  * 搜索内容
	  * @property {Object} evt 监听搜索框输入信息
	  */
	function interrogate(evt : any) {
		const { detail } = evt;
		if (!detail.value && inquireAbout) {
			console.log(detail.value)
			currentPage.value = 1;
			frinedList.value = [];
			init();
		}
	}
	/**
	 * 搜索
	 */
	function confirm() {
		if (search.value) {
			inquireAbout.value = search.value ? true : false;
			frinedList.value = [];
			init();
		} else {
			errorToast('请输入您要搜索的内容')
		}
	}
	function jump(id : number) {
		navTo('administrator/partner/partnerDetail?id=' + id)
	}
</script>

<style scoped lang="less">
	.add {
		width: 480rpx;
		height: 100rpx;
		background: linear-gradient(-10deg, #2C96F9, #1B91FF);
		border-radius: 5px;
		text-align: center;
		line-height: 100rpx;
		color: #fff;
		margin: 20rpx auto;
		font-size: 32rpx;
	}
</style>